package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.service.MarketGoodsService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yaml.snakeyaml.events.Event;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("wx/goods/")
public class WxGoodsController {

    @Autowired
    MarketGoodsService goodsService;

    @Autowired
    SecurityManager securityManager;


    /**
     * 此方法用于获取在售商品数目
     *
     * @return
     */
    @GetMapping("count")
    public Object count() {
        int count = goodsService.count();
        return ResponseUtil.ok(count);
    }

    /**
     * 此方法用于获取商品目录
     *
     * @param id
     * @return
     */
    @GetMapping("category")
    public Object category(Integer id) {

        Map<String, Object> result = goodsService.category(id);
        return ResponseUtil.ok(result);
    }

    /**
     * 此方法用于获取商品列表
     *
     * @param categoryId
     * @return
     */
    @GetMapping("list")
    public Object list(Integer categoryId, String keyword, boolean isNew, boolean isHot, Integer brandId, Integer page, Integer limit, String sort, String order) {

        //搜索后的商品列表
        if(keyword != null) {
            Map<String, Object> result = goodsService.listBySearch(keyword, page, limit, sort, order,categoryId);
            return ResponseUtil.ok(result);
        }
        //目录下的商品列表
        if (categoryId != null && categoryId != 0) {
            Map<String, Object> result = goodsService.list(categoryId, page, limit);
            return ResponseUtil.ok(result);
        }

        if (isNew) {
            Map<String, Object> result = goodsService.ListByIsNew(page, limit, sort, order);
            return ResponseUtil.ok(result);
        }

        if (isHot) {
            Map<String, Object> result = goodsService.ListByIsHot(page, limit, sort, order);
            return ResponseUtil.ok(result);
        }

        if (brandId != null) {
            Map<String, Object> result = goodsService.ListByBrandId(brandId, page, limit);
            return ResponseUtil.ok(result);
        }
        return null;

    }

    /**
     * 此方法用于获取商品详情
     *
     * @param id
     * @return
     */
    @GetMapping("detail")
    public Object detail(Integer id, HttpSession session) {

        Integer userId = (Integer) session.getAttribute("id");

        Map<String, Object> result = goodsService.detail(id,userId);

        return ResponseUtil.ok(result);
    }

    /**
     * 此方法用于获取商品详情页相关商品
     *
     * @param id
     * @return
     */
    @GetMapping("related")
    public Object related(Integer id) {

        List<MarketGoods> result = goodsService.related(id);

        return ResponseUtil.okList(result);
    }


}
